﻿// ## 2023/11/10 # 铃 # 重构 ##

namespace 军饷维护费
{
    // -------------------------------------------------------------------------------------------------------
    //---------------------------------------------------------------------------------------
    const bool 兵器维护费设置 = true;
    const bool 军队维护费设置 = true;
    const bool 士兵逃走设置 = true;
    const bool 兵器减少设置 = true;

    const int 军队基础维护费_城内 = 15;
    const int 军队基础维护费_出征 = 30;

    const int 兵装基础维护费 = 1;
    const int 兵器基础维护费 = 10;

    // -------------------------------------------------------------------------------------------------------

    class Main
    {
        Main()
        {
            pk::bind(112, pk::trigger112_t(扣除计算));
        }

        void 扣除计算(pk::force @force)
        {

            pk::list<pk::building @> list = ch::get_base_list(force);
            for (int i = 0; i < list.count; i++)
            {
                pk::building @building = list[i];
                int force_id = building.get_force_id();
                if (force_id < 0)
                    continue;
                if (force_id != force.get_id())
                    continue;
                auto building_p = ch::get_baseIA(building.get_id());

                //pk::trace(pk::format("城市名：{},势力名:{}", pk::decode(pk::get_name(building)), pk::decode(pk::get_name(force))));


                if (军队维护费设置)
                {
                    building_p.troops_expense = get_军饷(building);

                    pk::add_gold(building, -building_p.troops_expense, true);

                    // 士兵逃亡
                    if (士兵逃走设置 and -int(building_p.troops_expense) + pk::get_gold(building) < 0)
                    {
                        int deserter = pk::max(1, building.troops * ch::randint(5, 10) / 100);
                        ch::add_troops(building, -deserter, true, 102);
                        pk::add_energy(building, -30, true);
                        pk::add_gold(building, int(-1.0 * pk::get_gold(building)), true);

                        if (building.is_player())
                        {
                            pk::person @士兵 = pk::get_person(武将_士兵);
                            string building_name = pk::decode(pk::get_name(building));
                            pk::message_box(pk::encode(pk::format("报告主公,因\x1b[2x{}\x1b[0x无法支付军饷, 部分士兵乘夜出逃了.", building_name)), 士兵);
                        }
                    }
                }

                if (兵器维护费设置)
                {
                    building_p.weapon_expense = get_维护费(building);

                    pk::add_gold(building, -building_p.weapon_expense, true);

                    if (兵器减少设置 and -int(building_p.weapon_expense) + pk::get_gold(building) < 0)
                    {
                        // 兵器损坏
                        for (int k = 兵器_枪; k <= 兵器_战马; k++)
                        {
                            pk::add_weapon_amount(building, k, -int(int(building_p.weapon_expense) - pk::get_gold(building) * 100.0 / pk::get_equipment(k).gold_cost), true);
                        }

                        if (building.is_player())
                        {
                            pk::person @士兵 = pk::get_person(武将_士兵);
                            string building_name = pk::decode(pk::get_name(building));
                            pk::message_box(pk::encode(pk::format("报告主公,\x1b[2x{}\x1b[0x的军备军械维护费不足,部分兵装出现了损坏的情况.", building_name)), 士兵);
                        }
                    }
                }
            //pk::trace(pk::format("城市名：{},building_p.troops_expense：{},building_p.weapon_expense{}", pk::decode(pk::get_name(building)), building_p.troops_expense, building_p.weapon_expense));

            }
        }
    } Main main;

    int get_buff(pk::building @building)
    {
        array<int> difficult = { 10, 20, 30 };
        int difficult_buff = difficult[pk::get_scenario().difficulty];
        int count = pk::get_city_count(pk::get_force(building.get_force_id()));
        return difficult_buff + (count >= 3 ? 0 : 20 - 10 * (count - 1));
    }

    int get_军饷(pk::building @building)
    {
        int n = 0;
        int sum_unit_troops = 0;
        int force_id = building.get_force_id();
        if (force_id == -1 or force_id >= 势力_末)
            return 0;
        pk::force @force = pk::get_force(force_id);

        array<pk::unit @> unit_arr = pk::list_to_array(pk::get_unit_list(force));
        for (int i = 0; i < int(unit_arr.length); i++)
        {
            pk::unit @unit = unit_arr[i];
            if (building.get_id() == pk::get_service(unit))
                sum_unit_troops += int(unit.troops);
        }

        n = int(building.troops * 军队基础维护费_城内 / 10000.f + sum_unit_troops * 军队基础维护费_出征 / 10000.f);

        if (!building.is_player() and building.get_force_id() >= 0 and building.get_force_id() < 势力_末)
        {
            if (pk::get_force(building.get_force_id()).kunshu == 武将_于夫罗 or pk::get_force(building.get_force_id()).kunshu == 武将_轲比能)
                n = int(n * 0.1);
            else n = int(n * (100 - get_buff(building)) / 100);

        }

        return n;
    }

    int get_维护费(pk::building @building)
    {
        int n = 0;
        for (int i = 兵器_枪; i <= 兵器_战马; i++)
        {
            n += int(pk::get_equipment(i).gold_cost * pk::get_weapon_amount(building, i) / 10000.0 / 100.0 * 兵装基础维护费);
            //pk::trace(pk::format("城市名：{},n:{},兵装：{},数量{}", pk::decode(pk::get_name(building)), n,i ,pk::get_weapon_amount(building, i)));
        }

        for (int i = 兵器_冲车; i <= 兵器_斗舰; i++)
        {

            if (i == 兵器_走舸) continue;
            n += int(pk::get_equipment(i).gold_cost * pk::get_weapon_amount(building, i) / 10000.0 * 兵器基础维护费);
           // pk::trace(pk::format("城市名：{},n:{},兵器：{},数量{}", pk::decode(pk::get_name(building)), n,i ,pk::get_weapon_amount(building, i)));

        }
        if (city_tech::is_building_force_has_city(building, 8))
            n = int(n * 0.65f);

        if (!building.is_player() and building.get_force_id() >= 0 and building.get_force_id() < 势力_末)
        {
            if (pk::get_force(building.get_force_id()).kunshu == 武将_于夫罗 or pk::get_force(building.get_force_id()).kunshu == 武将_轲比能)
                n = int(n * 0.1);
            else n = int(n * (100 - get_buff(building)) / 100);
        }

        return n;
    }
}
